package cn.vihub.controller;
import cn.vihub.param.ShopUserParam;
import cn.vihub.pojo.ShopRole;
import cn.vihub.pojo.ShopUser;
import cn.vihub.service.ShopRoleService;
import cn.vihub.service.ShopUserService;
import cn.vihub.service.ShopUserToRoleService;
import cn.vihub.utils.Result;
import cn.vihub.vo.ShopUserVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
* 用户数据表(shop_user)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/shop-user/user")
public class UserController {
/**
* 服务对象
*/
    @Autowired
    private ShopUserService shopUserService;

    @Autowired
    private ShopUserToRoleService shopUserToRoleService;

    @Autowired
    private ShopRoleService shopRoleService;


    /**
     * @author: 秋晚
     * @description:分页模糊查询用户名列表数据
     * @params: [cn.vihub.param.ShopUserParam]
     * @return: cn.vihub.utils.Result<com.baomidou.mybatisplus.core.metadata.IPage<cn.vihub.pojo.ShopUser>>
     * @date: 2025/7/18 17:17
     */
    @GetMapping("/list")
    public Result<IPage<ShopUser>> list(ShopUserParam shopUserParam){
        return Result.success(shopUserService.selectPage(shopUserParam));
    }



    /**
     * @author: 秋晚
     * @description:添加及编辑用户
     * @params: [cn.vihub.param.ShopUserParam]
     * @return: cn.vihub.utils.Result<java.lang.Boolean>
     * @date: 2025/7/18 17:17
     */
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody ShopUserParam shopUserParam){
        //判断布尔值返回的是否为true  若是则成功  否则返回error
        if(shopUserService.save(shopUserParam)){
            return Result.success();
        }else{
            return Result.error(500,"添加失败");
        }
    }


    /**
     * @author: 秋晚
     * @description:根据id删除用户
     * @params: [java.lang.Integer]
     * @return: cn.vihub.utils.Result<java.lang.Boolean>
     * @date: 2025/7/18 17:18
     */
    @DeleteMapping("deleteById/{id}")
    public Result<Boolean> deleteById(@PathVariable(name = "id") Integer id){
        return Result.success(shopUserService.removeById(id));
    }



    /**
     * @author: 秋晚
     * @description:用户绑定角色数据
     * @params: [cn.vihub.param.ShopUserParam]
     * @return: cn.vihub.utils.Result<java.lang.Boolean>
     * @date: 2025/7/19 11:25
     */
    @PostMapping("binRole")
    public Result<Boolean> binRole(@RequestBody ShopUserParam shopUserParam){
        return Result.success(shopUserToRoleService.binRole(shopUserParam));
    }



    /**
     * @author: 秋晚
     * @description:根据userId获取旗下角色
     * @params: [java.lang.Integer]
     * @return: cn.vihub.utils.Result<java.util.List<cn.vihub.pojo.ShopRole>>
     * @date: 2025/7/19 11:39
     */
    @GetMapping("getRoleByUserId/{userId}")
    public Result<List<ShopRole>> getRoleByUserId(@PathVariable(name = "userId") Integer userId){
        return Result.success(shopRoleService.getRoleByUserId(userId));
    }

    /**
     * @author: 秋晚
     * @description:根据userId查询用户详情
     * @params: [java.lang.Integer]
     * @return: cn.vihub.utils.Result<cn.vihub.vo.ShopUserVo>
     * @date: 2025/7/21 19:12
     */
    @GetMapping("getUserDetail/{userId}")
    public Result<ShopUserVo> getUserDetail(@PathVariable(name = "userId") Integer userId){
        return Result.success(shopUserService.getUserDetail(userId));
    }

    /**
     * @author: 秋晚
     * @description:根据用户名查询用户
     * @params: [java.lang.String]
     * @return: cn.vihub.pojo.ShopUser
     * @date: 2025/7/21 19:15
     */
    @GetMapping("getUserByUsername/{username}")
    public ShopUser getUserByUsername(@PathVariable(name = "username") String username){
        return shopRoleService.getUserByUsername(username);
    }

}
